package com.lh_travel.lohas.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;

import com.lh_travel.lohas.R;
import com.lh_travel.lohas.activity.webview.NormalWebviewActivity;
import com.lh_travel.lohas.activity.webview.WebViewActivity;
import com.lh_travel.lohas.adapter.BaseRecyclerAdapter;
import com.lh_travel.lohas.adapter.BaseRecyclerHolder;
import com.lh_travel.lohas.base.api;
import com.lh_travel.lohas.base.baseActivity;
import com.lh_travel.lohas.domain.Comment;
import com.lh_travel.lohas.domain.CommentIntentBean;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxListCallback;

import java.util.List;

public class OrderActivity extends baseActivity {

    private Toolbar toolbar;
    private ImageButton img_back;
    private RecyclerView rcy_order;
    private List<Comment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_order);
        findView();
        bindListner();
        ensureUI();
    }

    @Override
    public void findView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        img_back = (ImageButton) findViewById(R.id.img_back);
        rcy_order = (RecyclerView) findViewById(R.id.rcy_order);
    }

    @Override
    public void bindListner() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void ensureUI() {

        setSupportActionBar(toolbar);
        toolbar.setEnabled(true);

        new api(mContext).myOrder(new RxListCallback<List<Comment>>() {


            @Override
            public void onNext(Object tag, int code, String message, List<Comment> response) {

                if (code == 200) {

                    list = response;

                    initxrv();

                }

            }

            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }
        });

    }

    private void initxrv() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        rcy_order.setLayoutManager(layoutManager);

        BaseRecyclerAdapter<Comment> adapter = new BaseRecyclerAdapter<Comment>(mContext, list, R.layout.item_order) {
            @Override
            public void convert(BaseRecyclerHolder holder, Comment item, int position, boolean isScrolling) {

                holder.setImageByUrl2(R.id.img_pic, item.picture, R.drawable.default_bg640x300);

                final CommentIntentBean bean = new CommentIntentBean();

                bean.keyWord = "";
                bean.checkIn = "";
                bean.checkOut = "";
                bean.url = item.third_url;

                holder.setViewClickLisenter(R.id.rl_main, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(mContext, NormalWebviewActivity.class);
                        intent.putExtra("detailBean", bean);
                        startActivity(intent);
                        overridePendingTransition(R.anim.act_bottom_in, R.anim.act_top_out);

                    }
                });
            }
        };

        rcy_order.setAdapter(adapter);


    }
}
